package net.oschina.victor.liu.lemon.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import net.oschina.victor.liu.lemon.common.MessageException;
import net.oschina.victor.liu.lemon.entity.Role;
import net.oschina.victor.liu.lemon.service.RoleService;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(@RequestParam(value = "pageNo", required = false, defaultValue = "0") String pageNoStr,
			@RequestParam(value = "pageSize", required = false, defaultValue = "10") String pageSizeStr, String keyword,
			Model model) {
		int pageNo = 0;
		try {
			pageNo = Integer.parseInt(pageNoStr);
			if (pageNo < 0) {
				pageNo = 0;
			}
		} catch (NumberFormatException e) {
			pageNo = 0;
			e.printStackTrace();
		}
		int pageSize = 0;
		try {
			pageSize = Integer.parseInt(pageSizeStr);
			if (pageSize < 0) {
				pageSize = 10;
			}
		} catch (NumberFormatException e) {
			pageSize = 10;
			e.printStackTrace();
		}
		System.out.println("搜索关键字：" + keyword);
		if (keyword == null) {
			keyword = "";
		}
		model.addAttribute("keyword", keyword);
		Page<Role> page = roleService.list(keyword, pageNo, pageSize);
		model.addAttribute("page", page);
		return "role/index";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String input(@PathVariable("id") String idStr, Map<String, Object> map) {
		System.out.println("获取的ID" + idStr);
		try {
			Long id = Long.parseLong(idStr);
			if (id != null) {
				map.put("role", roleService.getRole(id));
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	
		return "role/edit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String postRole(Role role) {
		System.out.println("新增保存" + role);
		try {
			if (role == null || "".equals(role.getRole())) {
				System.out.println("出现错误了");
				return "edit";
			} else {
				System.out.println(role);
				roleService.save(role);
			}
		} catch (MessageException e) {
			e.printStackTrace();
		}
		return "redirect:/role/list";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
	public String putRole(Role role) {
		System.out.println("更新保存" + role);
		try {
			if (role == null || "".equals(role.getRole())) {
				System.out.println("出现错误了");
				return "edit";
			} else {
				System.out.println(role);
				roleService.update(role);
			}
		} catch (MessageException e) {
			e.printStackTrace();
		}
		return "redirect:/role/list";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.DELETE)
	public String deleteRole(@PathVariable("id") String idStr) {

		System.out.println("获取的ID" + idStr);
		try {
			Long id = Long.parseLong(idStr);
			if (id != null) {
				roleService.delete(id);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (MessageException e) {
			e.printStackTrace();
		}
		return "redirect:/role/list";
	}
}
